<?php

namespace App\Http\Controllers\Sample;

use App\Helpers\Code;
use App\Models\Sample\SampleTypeWorkflow;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class TypeWorkflowController extends Controller
{

    /**
     * @author lcy
     */
    public function __construct()
    {
        $this->_validators();
        $this->model = new SampleTypeWorkflow();
    }

    /**
     * 添加样本类型可选择的工作流
     * @param Request $request
     * @return array
     */
    public function addTypeWorkflow(Request $request)
    {
        try {
            $data = $request->only($this->model->getFillable());
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->addTypeWorkflow($data);
            $this->checked($model, ['null' => 'sample.workflow.dependence_non_exist']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑样本类型工作流
     * @return array
     */
    public function editTypeWorkflow(Request $request, $id)
    {
        try {
            $data = $request->only(['sample_type_id', 'workflow_id', 'product_id', 'is_default']);
            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editTypeWorkflow($data, $id);
            $this->checked($model, [
                'null' => 'common.delete_failure',
                Code::INSIDE_ERROR => 'sample.type_not_exist',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 删除样本类型工作流
     * @param $id
     */
    public function deleteTypeWorkflow($id)
    {
        try {
            $model = $this->model->deleteTypeWorkflow($id);
            $this->checked($model, ['null' => 'common.delete_failure']);
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validators()
    {
        $this->rules = [
            'sample_type_id' => 'bail|required|numeric',
            'workflow_id' => 'bail|required|numeric',
            'product_id' => 'nullable|numeric',
            'is_default' => 'bail|required|in:0,1',
        ];

        $this->messages = [
            'sample_type_id.required' => trans('sample.workflow.sample_type_id_required'),
            'sample_type_id.numeric' => trans('sample.workflow.sample_type_id_numeric'),
            'workflow_id.required' => trans('sample.workflow.workflow_id_required'),
            'workflow_id.numeric' => trans('sample.workflow.workflow_id_numeric'),
            'product_id.numeric' => trans('sample.workflow.product_id_numeric'),
            'is_default.required' => trans('sample.workflow.is_default_required'),
            'is_default.in' => trans('sample.workflow.is_default_in'),
        ];
    }
}
